window.onload=function() {
    // 账户设置隐藏显示
    // 第一页
    var header_hidden = document.querySelector(".header-hidden");
    var number_fit = document.querySelector(".header-box-1").children[1];
    var q = document;
    number_fit.addEventListener("mouseover", function () {
        header_hidden.style.display = "block";
    });
    q.addEventListener("click", function () {
        header_hidden.style.display = "none";
    });
    header_hidden.addEventListener("click", function (e) {
        e.stopPropagation();
    })
// 第二页
    var header_hidden1 = document.querySelectorAll(".header-hidden")[1];
    var number_fit1 = document.querySelectorAll(".header-box-1")[1].children[1];
    var q = document;
    number_fit1.addEventListener("mouseover", function () {
        header_hidden1.style.display = "block";
    });
    q.addEventListener("click", function () {
        header_hidden1.style.display = "none";
    });
    header_hidden1.addEventListener("click", function (e) {
        e.stopPropagation();
    })
    // 跳转到首页
    var all_main=document.querySelector(".nav-top-box-1").children[0]
    all_main.addEventListener("click", function () {
        window.location.href="/";
    });
    // 跳转到首页
    var all_main1=document.querySelector(".all-main");
    all_main1.addEventListener("click", function () {
        window.location.href="/";
    });
    // 跳转到登录页面或者个人中心
    var nav_top_box_2=document.querySelector(".nav-top-box-2").children[0];
    var text_1=nav_top_box_2.innerHTML;
    console.log(text_1)
    nav_top_box_2.addEventListener("click",function() {
        if (text_1 == "登录") {
            window.location.href = "/User/login";
        } else {
            window.location.href = "/User/userInterface";
        }
    })
    // 点击跳转修改密码
    var change=document.querySelectorAll(".changepassword");
    change[0].addEventListener("click",function() {
        window.location.href = "/User/updatePassword"
    })
    change[1].addEventListener("click",function() {
        window.location.href = "/User/updatePassword"
    })
    //点击退出跳转到主页
    var exit = document.querySelector(".exit")
    exit.addEventListener("click",function(){
        $.ajax({
            type: "post",
            url: "/User/loginOut",
            // data: {
            // },
            success: function (data) {
                // alert("退出")
                window.location.href="/";

            },error: function () {
                console.log("調取接口失敗")
            }
        })
    })
    // 跳转到登录
    var exit2 = document.querySelector(".exit2")
    exit2.addEventListener("click",function(){
        $.ajax({
            type: "post",
            url: "/User/loginOut",
            // data: {
            // },
            success: function (data) {
                // alert("退出")
                window.location.href="/";

            },error: function () {
                console.log("調取接口失敗")
            }
        })
    })
    // 点击跳转个人资料
    var address_3=document.querySelector(".user-news-box-2-1");
    address_3.addEventListener("click",function(){
        window.location.href="/User/userInformation";
    });
    // 跳转
    var part1=document.querySelector(".all.part1");
    var part2=document.querySelector(".all.part2");
    var address_1=document.querySelectorAll(".user-news-box-1-1")[1];
    var address_2=document.querySelectorAll(".main-left")[1].children[3]
    address_1.addEventListener("click",function(){
        part1.style.display="block";
        part2.style.display="none";
        fenYe(id.innerHTML);
    })
    address_2.addEventListener("click",function(){
        part1.style.display="block";
        part2.style.display="none";
        fenYe(id.innerHTML);
    })
    // 跳转到用户界面首页
    var main = document.querySelector(".header-box-1").children[0];
    main.addEventListener("click",function(){
       window.location.href="/User/userInterface"
    })
    var id=document.querySelector(".id");
    console.log(id.innerHTML)



    // 分页查询
    var tb=document.querySelector("tbody");
    function fenYe(x) {
        tb.innerHTML = "";
        $.ajax({
            type: "post",
            url: "/Address/queryAllAddressee",
            data: {
                userId: x
            },
            success: function (data) {
                for (let i = 0; i < data.length; i++) {
                    let list=`<tr>
                                   <td>${data[i].addressee}</td>
                                   <td>${data[i].address}</td>
                                  <td>${data[i].addresseeTele}</td>
                                  <td><button class='delete'>删除</button><button class='modify'>修改</button></td>
                                  <td><button class='default'>默认</button></td>
                            </tr>`
                    tb.insertAdjacentHTML("beforeend",list);
                }
                // 删除
                var delete1=document.querySelectorAll(".delete");
                for (let i = 0; i < data.length; i++) {
                    delete1[i].addEventListener("click",function(){
                        del(data[i].addressId);
                    })
                }
                // 点击修改
                var modify1=document.querySelectorAll(".modify");
                var part1=document.querySelector(".all.part1");
                var part2=document.querySelector(".all.part2");

                var address1 = document.querySelector(".address-address-1");
                var name1 = document.querySelector(".address-name-1");
                var phone1 = document.querySelector(".address-phone-1");
                for (let i=0;i<data.length;i++){
                    modify1[i].addEventListener("click",function(){
                        part1.style.display="none";
                        part2.style.display="block";
                        address1.value=data[i].address;
                        name1.value=data[i].addressee;
                        phone1.value=data[i].addresseeTele;
                        fenYe1(id.innerHTML);
                        $(".keep").eq(1).off().on("click",function(){
                            keep_1(data[i].addressId)
                        })
                    })
                }

            }
        })
    }
    fenYe(id.innerHTML);
    // 新增地址
    function keep(x) {
        var keep = document.querySelector(".keep");
        var address = document.querySelector(".address-address");
        var name = document.querySelector(".address-name");
        var phone = document.querySelector(".address-phone");
        keep.addEventListener("click", function () {
            $.ajax({
                type: "post",
                url: "/Address/addAddressee",
                data: {
                    address: address.value,
                    addressee: name.value,
                    addresseeTelephone: phone.value,
                    userId: x
                },
                success: function (data) {
                    if(data==1) {
                        alert("保存成功")
                        console.log(data);
                        fenYe(id.innerHTML);
                        address.value = "";
                        name.value = "";
                        phone.value = "";
                    }else{
                        if(name.value.length>=6||name.value.length<=1){
                            alert("名字长度出错")
                        }else if(phone.value[0]!==1&&phone.value.length!==11){
                            alert("手机号格式出错");
                        }
                    }
                }
            })
        })
    }
    keep(id.innerHTML);
    // 删除收货地址
    // var delete1=document.querySelectorAll(".delete");


function del(x) {
    $.ajax({
        type: "post",
        url: "/Address/deleteAddressee",
        data: {
            addresseeId: x
        },
        success: function (data) {
            alert("删除成功");
            fenYe(id.innerHTML);
        }
    })
}
    function del1(x) {
        $.ajax({
            type: "post",
            url: "/Address/deleteAddressee",
            data: {
                addresseeId: x
            },
            success: function (data) {
                alert("删除成功");
                fenYe1(id.innerHTML);
            }
        })
    }
// 分页查询
    var tb1=document.querySelectorAll("tbody")[1];
    function fenYe1(x) {
        tb1.innerHTML = "";
        $.ajax({
            type: "post",
            url: "/Address/queryAllAddressee",
            data: {
                userId: x
            },
            success: function (data) {
                for (let i = 0; i < data.length; i++) {
                    let list=`<tr>
                                   <td>${data[i].addressee}</td>
                                   <td>${data[i].address}</td>
                                  <td>${data[i].addresseeTele}</td>
                                  <td><button class='delete-1'>删除</button><button class='modify-1'>修改</button></td>
                                  <td><button class='default-1'>默认</button></td>
                            </tr>`
                    tb1.insertAdjacentHTML("beforeend",list);
                }
                // 删除
                var delete2=document.querySelectorAll(".delete-1");
                for (let i = 0; i < data.length; i++) {
                    delete2[i].addEventListener("click",function(){
                        del1(data[i].addressId);
                    })
                }
                // 点击修改
                var modify2=document.querySelectorAll(".modify-1");
                var part1=document.querySelector(".all.part1");
                var part2=document.querySelector(".all.part2");

                var address1 = document.querySelector(".address-address-1");
                var name1 = document.querySelector(".address-name-1");
                var phone1 = document.querySelector(".address-phone-1");
                for (let i=0;i<data.length;i++){
                    modify2[i].addEventListener("click",function(){
                        part1.style.display="none";
                        part2.style.display="block";
                        address1.value=data[i].address;
                        name1.value=data[i].addressee;
                        phone1.value=data[i].addresseeTele;
                        fenYe(id.innerHTML);
                        $(".keep").eq(1).off().on("click",function(){
                            keep_1(data[i].addressId)
                        })
                    })
                }

            }
        })
    }
    fenYe1(id.innerHTML);
    // 修改地址
    function keep_1(x) {
        var address1 = document.querySelector(".address-address-1");
        var name1 = document.querySelector(".address-name-1");
        var phone1 = document.querySelector(".address-phone-1");
            $.ajax({
                type: "post",
                url: "/Address/updateAddressee",
                data: {
                    address: address1.value,
                    addressee: name1.value,
                    addresseeTele: phone1.value,
                    addresseeId:x
                },
                success: function (data) {
                    if(data==1) {
                        alert("修改成功")
                        console.log(data);
                        fenYe1();
                    }else{
                        if(name1.value.length>=6||name1.value.length<=1){
                            alert("名字长度出错")
                        }else if(phone1.value[0]!==1&&phone1.value.length!==11){
                            alert("手机号格式出错");
                        }
                    }
                }
            })
    }
}